//
//  HR_originalImageViewController.m
//  HR_PickKit
//
//  Created by KK on 2017/7/24.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "HR_originalImageViewController.h"

@interface HR_originalImageViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *imageScrollView;

@property (strong, nonatomic) PHFetchResult *assetFetchResult;

@property (assign, nonatomic) NSUInteger currentIndex;

@end

@implementation HR_originalImageViewController

#pragma mark - ----------   Lifecycle（生命周期） ----------
- (instancetype)initWithPHFetchResult:(PHFetchResult *)assetFetchResult currentIndex:(NSUInteger)index {
    self = [super init];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        _assetFetchResult = assetFetchResult;
        _currentIndex = index;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - ---------- Private Methods（私有方法） ----------

#pragma mark initliaze data(初始化数据)
- (void)configUI{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.imageScrollView setContentSize:CGSizeMake(self.assetFetchResult.count * SCREEN_WIDTH, SCREEN_HEIGHT)];
    for (NSInteger i = 0; i < self.assetFetchResult.count; i++) {
        UIImageView *imageView =  [[UIImageView alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelAction)];
        [imageView addGestureRecognizer:tap];
        [self.imageScrollView addSubview:imageView];
    }
    
    [self.imageScrollView setContentOffset:CGPointMake(SCREEN_WIDTH * self.currentIndex, 0) animated:NO];
    
    [[PHImageManager defaultManager] requestImageForAsset:[self.assetFetchResult objectAtIndex:self.currentIndex] targetSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT) contentMode:PHImageContentModeAspectFit options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        UIImageView *imgView = (UIImageView *)[self.imageScrollView.subviews objectAtIndex:self.currentIndex];
        imgView.image = result;
    }];

    
    
}
#pragma mark config control（布局控件）

#pragma mark networkRequest (网络请求)

#pragma mark actions （点击事件）
/** 点击消失 */
- (void)cancelAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark IBActions （点击事件xib）

#pragma mark - ---------- Public Methods（公有方法） ----------

#pragma mark self declare （本类声明）

#pragma mark override super （重写父类）

#pragma mark setter （重写set方法）

#pragma mark - ---------- Protocol Methods（代理方法） ----------
#pragma mark - UIScrollViewDelegate  -

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSUInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    [[PHImageManager defaultManager] requestImageForAsset:[self.assetFetchResult objectAtIndex:index] targetSize:CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT) contentMode:PHImageContentModeAspectFit options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        for (NSUInteger i = index; i < index + 1; i ++) {
            UIImageView *imgView = (UIImageView *)[scrollView.subviews objectAtIndex:index];
            imgView.image = result;
        }
    }];
}



#pragma mark - ---------- Lazy Loading（懒加载） ----------

@end
